package com.vimi8.ebb.service;

import com.vimi8.ebb.model.DataReport;
import com.vimi8.ebb.param.DataReportQueryParam;
import org.apache.ibatis.annotations.*;

import java.util.HashMap;
import java.util.List;

/**
 * 数据报告service
 * Created by xiehuang on 2017/4/5.
 */
@Mapper
public interface DataReportService {
    /**
     * 添加数据报告
     *
     * @param dataReport 数据报告对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO data_report(id, general_situation, platform_statistics, product_analysis, subject_analysis, work_proposal, platform_id, address_code, quarter, create_time, update_time) " +
            "VALUES(#{id}, #{generalSituation}, #{platformStatistics}, #{productAnalysis}, #{subjectAnalysis}, #{workProposal}, #{platformId}, #{addressCode}, #{quarter}, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP())")
    public int insertDataReport(DataReport dataReport);

    /**
     * 修改数据报告
     *
     * @param dataReport 数据报告对象
     * @return 影响的行数
     */
    @Update("<script>" +
            "UPDATE data_report " +
            "<set>" +
            "<if test=\"generalSituation!=null and generalSituation!=''\">general_situation = #{generalSituation},</if>" +
            "<if test=\"platformStatistics!=null and platformStatistics!=''\">platform_statistics = #{platformStatistics},</if>" +
            "<if test=\"productAnalysis!=null and productAnalysis!=''\">product_analysis = #{productAnalysis},</if>" +
            "<if test=\"subjectAnalysis!=null and subjectAnalysis!=''\">subject_analysis = #{subjectAnalysis},</if>" +
            "<if test=\"workProposal!=null and workProposal!=''\">work_proposal = #{workProposal},</if>" +
            "<if test=\"platformId!=null and platformId!=''\">platform_id = #{platformId},</if>" +
            "<if test=\"addressCode!=null and addressCode!=''\">address_code = #{addressCode},</if>" +
            "<if test=\"quarter!=null and quarter!=''\">quarter = #{quarter},</if>" +
            "update_time = CURRENT_TIMESTAMP()," +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    public int updateDataReport(DataReport dataReport);

    /**
     * 删除数据报告
     *
     * @param id
     * @return 影响的行数
     */
    @Update("DELETE FROM data_report WHERE id=#{id}")
    public int deleteDataReport(String id);

    /**
     * 查询数据报告的条数
     *
     * @param param 查询参数
     * @return 数据报告的条数
     */
    @Select("<script>" +
            "SELECT count(*) FROM data_report " +
            "<where>" +
            "<if test=\"param.addressCode!=null and param.addressCode!='' \">AND address_code LIKE CONCAT('%',#{param.addressCode},'%') </if> " +
            "<if test=\"param.quarter!=null and param.quarter!='' \">AND quarter = #{param.quarter} </if> " +
            "</where>" +
            "</script>")
    public int getDataReportCount(@Param(value = "param") DataReportQueryParam param);

    /**
     * 查询数据报告
     *
     * @param offset               偏移
     * @param size                 每页条数
     * @param param 查询参数对象
     * @return 数据报告的列表
     */
    @Select("<script>" +
            "SELECT id, general_situation AS generalSituation, platform_statistics AS platformStatistics, product_analysis AS productAnalysis, " +
            "subject_analysis AS subjectAnalysis, work_proposal AS workProposal, platform_id AS platformId, address_code AS addressCode, " +
            "quarter, DATE_FORMAT(create_time,'%Y-%m-%d %T') AS createTime, DATE_FORMAT(update_time,'%Y-%m-%d %T') AS updateTime " +
            "FROM data_report " +
            "<where>" +
            "<if test=\"param.addressCode!=null and param.addressCode!='' \">AND address_code LIKE CONCAT('%',#{param.addressCode},'%') </if> " +
            "<if test=\"param.quarter!=null and param.quarter!='' \">AND quarter = #{param.quarter} </if> " +
            "</where>" +
            "LIMIT #{offset}, #{size}" +
            "</script>")
    public List<DataReport> getDataReports(
            @Param(value = "offset") int offset,
            @Param(value = "size") int size,
            @Param(value = "param") DataReportQueryParam param);

    /**
     * 根据ID查询数据报告
     *
     * @param id
     * @return 数据报告
     */
    @Select("SELECT id, general_situation AS generalSituation, platform_statistics AS platformStatistics, product_analysis AS productAnalysis, " +
            "subject_analysis AS subjectAnalysis, work_proposal AS workProposal, platform_id AS platformId, address_code AS addressCode, " +
            "quarter, DATE_FORMAT(create_time,'%Y-%m-%d %T') AS createTime, DATE_FORMAT(update_time,'%Y-%m-%d %T') AS updateTime " +
            "FROM data_report WHERE id = #{id}")
    public DataReport getDataReportById(String id);

}
